package com.piggy.logi.sys.controller;

import com.piggy.common.core.domain.R;
import com.piggy.common.core.utils.poi.ExcelUtil;
import com.piggy.common.core.web.controller.BaseController;
import com.piggy.common.core.web.page.TableDataInfo;
import com.piggy.common.log.annotation.Log;
import com.piggy.common.log.annotation.LogiLog;
import com.piggy.common.log.enums.BusinessType;
import com.piggy.common.satoken.utils.SecurityUtils;
import com.piggy.logi.sys.bo.LogiWaybillSignEditBo;
import com.piggy.logi.sys.bo.LogiWaybillSignQueryBo;
import com.piggy.logi.sys.service.ILogiWaybillSignService;
import com.piggy.logi.sys.vo.LogiWaybillSignVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 运单签收Controller
 *
 * @author piggy
 * @date 2023-09-19
 */
@Api(value = "运单签收控制器", tags = {"运单签收管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/sys/sign")
public class LogiWaybillSignController extends BaseController {

    private final ILogiWaybillSignService iLogiWaybillSignService;

    /**
     * 查询运单签收列表
     */
    @ApiOperation("查询运单签收列表")
    //@SaCheckPermission("sys:sign:list")
    @PostMapping("/list")
    public TableDataInfo<LogiWaybillSignVo> list(@Validated @RequestBody LogiWaybillSignQueryBo bo) {
        return iLogiWaybillSignService.queryPageList(bo);
    }

    /**
     * 导出运单签收列表
     */
    @ApiOperation("导出运单签收列表")
    //@SaCheckPermission("sys:sign:export")
    @LogiLog(title = "运单签收", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(@Validated LogiWaybillSignQueryBo bo, HttpServletResponse response) throws IOException {
        List<LogiWaybillSignVo> list = iLogiWaybillSignService.queryList(bo);
        ExcelUtil<LogiWaybillSignVo> util = new ExcelUtil<LogiWaybillSignVo>(LogiWaybillSignVo.class);
        util.exportExcel(response, list, "运单签收");
    }

    /**
     * 获取运单签收详细信息
     */
    @ApiOperation("获取运单签收详细信息")
    //@SaCheckPermission("sys:sign:query")
    @GetMapping("/{signedId}")
    public R<LogiWaybillSignVo> getInfo(@NotNull(message = "主键不能为空")
                                        @PathVariable("signedId") Long signedId) {
        return R.ok(iLogiWaybillSignService.queryById(signedId));
    }

    /**
     * 新增运单签收
     */
    @ApiOperation("新增运单签收")
    //@SaCheckPermission("sys:sign:add")
    @LogiLog(title = "运单签收", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<LogiWaybillSignVo> add(@Validated @RequestBody LogiWaybillSignEditBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        return R.ok(iLogiWaybillSignService.insertByAddBo(bo));
    }

    /**
     * 修改运单签收
     */
    @ApiOperation("修改运单签收")
    //@SaCheckPermission("sys:sign:edit")
    @LogiLog(title = "运单签收", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<LogiWaybillSignVo> edit(@Validated @RequestBody LogiWaybillSignEditBo bo) {
        return R.ok(iLogiWaybillSignService.updateByEditBo(bo));
    }

    /**
     * 删除运单签收
     */
    @ApiOperation("删除运单签收")
    //@SaCheckPermission("sys:sign:remove")
    @LogiLog(title = "运单签收", businessType = BusinessType.DELETE)
    @DeleteMapping("/{signedIds}")
    public R<Integer> remove(@NotEmpty(message = "主键不能为空")
                             @PathVariable Long[] signedIds) {
        return R.ok(iLogiWaybillSignService.deleteWithValidByIds(Arrays.asList(signedIds), true) ? 1 : 0);
    }
}
